package net.kts.attraction.subway.smallpass.sqlite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.kts.attraction.subway.smallpass.model.infomation.MultiLineStation;
import net.kts.attraction.subway.smallpass.model.infomation.MultiLineTime;
import net.kts.attraction.subway.smallpass.model.infomation.StationInfo;
import net.kts.attraction.subway.smallpass.model.infomation.StationTransfer;
import net.kts.attraction.subway.smallpass.model.infomation.StationTransferTime;
import net.kts.attraction.subway.smallpass.model.infomation.TransferInfo;
import net.kts.attraction.subway.smallpass.model.infomation.TransferTime;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SubwayDaoSQLite extends SQLiteOpenHelper {
	
	// 지하철 갈래길 소요시간     // key, value = 갈래길 역코드 , 갈래길 소요시간 model
	private Map<Integer, List<MultiLineTime>> multiLineTimeMap = new HashMap<Integer, List<MultiLineTime>>();
	// 갈래길 정보    // key, value= 갈래길 역코드 , 갈래길 정보 model
	private Map<Integer, MultiLineStation> multiLineStationMap = new HashMap<Integer, MultiLineStation>();
	
	// 환승역 정보들
	private Map<Integer, StationTransfer> stationTransferMap = new HashMap<Integer, StationTransfer>();

	// 환승 시간 정보  key, value = stationCd, 환승역 소요시간정보
	private Map<Integer, StationTransferTime> stationTransferTimeMap = new HashMap<Integer, StationTransferTime>();
	
	// 역정보  key, value = 역코드 , 역정보
	private Map<Integer, StationInfo> stationInfoMap = new HashMap<Integer, StationInfo>();
	// 기본 역정보
	
	public SubwayDaoSQLite(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	// 갈래길 소요시간
	public void defaulmultiLineTime() {


		Cursor cursor = getReadableDatabase().rawQuery("select * from TBL_MULTILINE_TIME", null);

		cursor.moveToFirst();

		MultiLineTime multiLineTime = null;
		
		List<MultiLineTime> multiLineTimeList = null;

		int multiLineStationCode = 0;
		int prevStationCode = 0;
		int nextStationCode = 0;
		int multiLineTimes = 0;

		int saveStationCode = 0;
		int currentStationCode = 0;

		while(!cursor.isAfterLast()) {

			multiLineTime = new MultiLineTime();

			multiLineStationCode = cursor.getInt(0);
			prevStationCode = cursor.getInt(1);
			nextStationCode = cursor.getInt(2);
			multiLineTimes = cursor.getInt(3);

			currentStationCode = multiLineStationCode;

			if(saveStationCode != currentStationCode) {

				multiLineTimeList = new ArrayList<MultiLineTime>();

				saveStationCode = currentStationCode;

				multiLineTime.setMultiLineStationCode(multiLineStationCode);
				multiLineTime.setPrevStationCode(prevStationCode);
				multiLineTime.setNextStationCode(nextStationCode);
				multiLineTime.setMultiLineTime(multiLineTimes);

				multiLineTimeList.add(multiLineTime);

				multiLineTimeMap.put(saveStationCode, multiLineTimeList);

			}

			else {


				multiLineTime.setMultiLineStationCode(multiLineStationCode);
				multiLineTime.setPrevStationCode(prevStationCode);
				multiLineTime.setNextStationCode(nextStationCode);
				multiLineTime.setMultiLineTime(multiLineTimes);

				multiLineTimeList.add(multiLineTime);

				multiLineTimeMap.put(saveStationCode, multiLineTimeList);

			}

			cursor.moveToNext();
		}


	}
	
	// 갈래길 정보
	public void defaultMultiLineSation() {
		Cursor cursor = getReadableDatabase().rawQuery("select * from TBL_MULTILINE_STATION", null);
		
		cursor.moveToFirst();
		
		int multiLineStationCode = 0;
		int nextStationCode = 0;
		
		int saveStationCode = 0;
		int currentStationCode = 0;
		
		MultiLineStation multiLineStation = null;
		List<Integer> nextStationCodeList = null;
		
		while(!cursor.isAfterLast()) {
			
			multiLineStationCode = cursor.getInt(0);
			nextStationCode = cursor.getInt(1);
			
			currentStationCode = multiLineStationCode;
			
			if(currentStationCode != saveStationCode) {
				saveStationCode = currentStationCode;
				
				multiLineStation = new MultiLineStation();
				nextStationCodeList = new ArrayList<Integer>();
				
				nextStationCodeList.add(nextStationCode);
				
				multiLineStation.setMultiLineStationCode(multiLineStationCode);
				multiLineStation.setNextStationCodeList(nextStationCodeList);
				
				multiLineStationMap.put(saveStationCode, multiLineStation);
				
				
			}
			
			else {
				
				nextStationCodeList.add(nextStationCode);
				
				multiLineStation.setMultiLineStationCode(multiLineStationCode);
				multiLineStation.setNextStationCodeList(nextStationCodeList);
				
				multiLineStationMap.put(saveStationCode, multiLineStation);
				
			}
			
			
			
			cursor.moveToNext();
		}
		
	}
	
	
	public void defaultTransferinfo() {
		
		Cursor cursor = getReadableDatabase().rawQuery("select * from TBL_TRANSFER_STATION", null);
		
		cursor.moveToFirst();
		
		StationTransfer stationTransfer = null;
		
		TransferInfo transferInfo = null;
		List<TransferInfo> transferInfoList = null;
		
		int stationCode = 0;
		String lineNumStationCode = null;
		String[] lineNumStationCodes = null;
		
		
		
		while(!cursor.isAfterLast()) {
			
			stationTransfer = new StationTransfer();
			transferInfoList = new ArrayList<TransferInfo>();
			
			stationCode = cursor.getInt(0);
			lineNumStationCode  = cursor.getString(1);
			
			lineNumStationCodes = lineNumStationCode.split(";");
			
			for(int indexI = 0; indexI < lineNumStationCodes.length; indexI += 2) {
				transferInfo = new TransferInfo();
				
				transferInfo.setLinenum(Integer.valueOf(lineNumStationCodes[indexI].trim()));
				transferInfo.setStationCode(Integer.valueOf(lineNumStationCodes[indexI+1].trim()));
				
				transferInfoList.add(transferInfo);
				
			}
			
			stationTransfer.setTransferInfoList(transferInfoList);
			
			stationTransferMap.put(stationCode, stationTransfer);
			
			cursor.moveToNext();
		}
		
		
	}
	
	public void defaultStationTransferTime() {
		
		Cursor cursor = getReadableDatabase().rawQuery("select * from TBL_STATION_TRANSFER order by station_cd ", null);
		
		cursor.moveToFirst();
		
		int stationCode = 0;
		int startLine = 0;
		int targetLine = 0;
		double time = 0;
		
		int saveStationCode = 0;
		int currentStationCode = 0;
		
		StationTransferTime stationTransferTime = null;
		TransferTime transferTime = null;
		List<TransferTime> transferTimeList = null;
		
		while(!cursor.isAfterLast()) {
			
			stationCode = cursor.getInt(0);
			startLine = cursor.getInt(1);
			targetLine = cursor.getInt(2);
			time = cursor.getDouble(3);
			
			currentStationCode = stationCode;
			
			if(saveStationCode != currentStationCode) {
				
				stationTransferTime = new StationTransferTime();
				transferTimeList = new ArrayList<TransferTime>();
				
				transferTime = new TransferTime();
				
				transferTime.setStartLineNum(startLine);
				transferTime.setEndLineNum(targetLine);
				transferTime.setTime(time);
				
				transferTimeList.add(transferTime);
				
				stationTransferTime.setTransferTimeList(transferTimeList);
				
				stationTransferTimeMap.put(stationCode, stationTransferTime);
				
				saveStationCode = currentStationCode;
			}
			else {
				
				transferTime = new TransferTime();
				
				transferTime.setStartLineNum(startLine);
				transferTime.setEndLineNum(targetLine);
				transferTime.setTime(time);
				
				transferTimeList.add(transferTime);
				
				stationTransferTime.setTransferTimeList(transferTimeList);
				
				stationTransferTimeMap.put(stationCode, stationTransferTime);
				
			}
			
			cursor.moveToNext();
		}
		
		
	}
	
	public void defaultStationInfos() {
		Cursor cursor = getReadableDatabase().rawQuery("select * from TBL_STATIONINFO", null);
		
		cursor.moveToFirst();
		
		int linenum = 0;
		int stationCode = 0;
		int upStationCode = 0;
		double upStationTime = 0;
		int downStationCode = 0;
		double downStationTime = 0;
		
		StationInfo stationInfo = null;
		
		while(!cursor.isAfterLast()) {
			
			stationInfo = new StationInfo();
			
			linenum = cursor.getInt(0);
			stationCode = cursor.getInt(1);
			upStationCode = cursor.getInt(2);
			upStationTime = cursor.getDouble(3);
			downStationCode = cursor.getInt(4);
			downStationTime = cursor.getDouble(5);
			
			stationInfo.setLineNum(linenum);
			stationInfo.setStationCode(stationCode);
			stationInfo.setUpStationCode(upStationCode);
			stationInfo.setUpStationTime(upStationTime);
			stationInfo.setDownStationCode(downStationCode);
			stationInfo.setDownStationTime(downStationTime);
			
			stationInfoMap.put(stationCode, stationInfo);
			
			
			cursor.moveToNext();
		}
		
	}

	public Map<Integer, List<MultiLineTime>> getMultiLineTimeMap() {
		return multiLineTimeMap;
	}

	public Map<Integer, MultiLineStation> getMultiLineStationMap() {
		return multiLineStationMap;
	}

	public Map<Integer, StationTransfer> getStationTransferMap() {
		return stationTransferMap;
	}

	public Map<Integer, StationTransferTime> getStationTransferTimeMap() {
		return stationTransferTimeMap;
	}

	public Map<Integer, StationInfo> getStationInfoMap() {
		return stationInfoMap;
	}
	
	

}
